package com.banciyuan.bcywebview.biz.detail.noveldetail.holder;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.base.util.BaseObserver;
import com.banciyuan.bcywebview.utils.universalimageloader.core.ImageLoader;
import com.banciyuan.bcywebview.utils.universalimageloader.utils.ImageOptions;

import de.greenrobot.daoexample.model.Complex;

/**
 * Created by squall on 2016/6/7.
 */
public class IndexHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    private ImageView index_img;
    private TextView index_title;
    private TextView index_content_book;
    private TextView index_content_chapter;
    private TextView index_end_status;
    private TextView index_tv;
    private RelativeLayout index_container;
    private View book_mark;
    private View has_book_mark;
    private String format1 = "%s  |";
    private String format2 = "%s";

    public IndexHolder(View view) {
        super(view);
        index_img = (ImageView) view.findViewById(R.id.index_img);
        index_title = (TextView) view.findViewById(R.id.index_title);
        index_content_book = (TextView) view.findViewById(R.id.index_content_book);
        index_content_chapter = (TextView) view.findViewById(R.id.index_content_chapter);
        index_end_status = (TextView) view.findViewById(R.id.index_end_status);
        index_tv = (TextView) view.findViewById(R.id.detail_index_click);
        book_mark = view.findViewById(R.id.detail_book_tv);
        has_book_mark = view.findViewById(R.id.detail_hasbook_tv);
        index_container = (RelativeLayout) view.findViewById(R.id.index_container);
    }

    public void initAction() {
        index_tv.setOnClickListener(this);
        index_img.setOnClickListener(this);
        index_title.setOnClickListener(this);
        book_mark.setOnClickListener(this);
        has_book_mark.setOnClickListener(this);
    }


    public void setData(Context mContext, Complex novel) {
        if (null != novel.getSet_data()) {
            index_container.setVisibility(View.VISIBLE);
            ImageLoader.getInstance().displayImage(novel.getSet_data().getCover(), index_img, ImageOptions.options_noneDisplay, null, null, true);
            index_title.setText(Html.fromHtml(novel.getSet_data().getTitle()));
            String text = "";
            if (novel.getSet_data().isSet_end()) {
                text = mContext.getString(R.string.series_done);
                index_end_status.setTextColor(mContext.getResources().getColorStateList(R.color.series_orange));
            } else {
                text = mContext.getString(R.string.series_ing);
                index_end_status.setTextColor(mContext.getResources().getColorStateList(R.color.series_green));
            }
            index_end_status.setText(text);
            String subscribe_num = String.format(format1, novel.getSet_data().getSubscribe_num());
            index_content_book.setText(subscribe_num);

            String chapter_num = String.format(format2, novel.getSet_data().getCount());
            index_content_chapter.setText(chapter_num);

            if (novel.getSet_data().isSubscribed()) {
                setBookMarked();
            } else {
                setBookUnMarked();
            }
        } else {
            index_container.setVisibility(View.GONE);
        }
        initAction();
    }

    public void setBookUnMarked() {
        book_mark.setVisibility(View.VISIBLE);
        has_book_mark.setVisibility(View.GONE);
    }

    public void setBookMarked() {
        book_mark.setVisibility(View.GONE);
        has_book_mark.setVisibility(View.VISIBLE);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.detail_index_click:
            case R.id.novel_page_num:
            case R.id.index_img:
            case R.id.index_title:
                BaseObserver.getInstance().notifyWatchers(BaseObserver.NOVEL_GO_INDEX);
                break;
            case R.id.detail_book_tv:
                BaseObserver.getInstance().notifyWatchers(BaseObserver.NOVEL_DO_BOOK);
                break;
            case R.id.detail_hasbook_tv:
                BaseObserver.getInstance().notifyWatchers(BaseObserver.NOVEL_DONOT_BOOK);
                break;
        }
    }


}
